package com.langChain4j.modelManagement.controller;

import com.base.api.entity.RestResponse;
import com.langChain4j.modelManagement.dto.ModelAccessConfigDTO;
import com.langChain4j.api.entity.ModelAccessConfig;
import com.langChain4j.modelManagement.query.ModelAccessConfigQuery;
import com.langChain4j.modelManagement.service.ModelAccessConfigService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 模型接入配置控制器
 *
 * @author linjun
 */
@RestController
@RequestMapping("/api/v1/model-config")
public class ModelAccessConfigController {

    @Autowired
    private ModelAccessConfigService modelAccessConfigService;

    /**
     * 新增模型接入配置
     *
     * @param dto 模型接入配置信息
     * @return RestResult 结果
     */
    @PostMapping("/add")
    public RestResponse<ModelAccessConfig> addModelAccessConfig(@Valid @RequestBody ModelAccessConfigDTO dto) {
        ModelAccessConfig accessConfig = modelAccessConfigService.addModelAccessConfig(dto);
        return RestResponse.<ModelAccessConfig>builder()
                .data(accessConfig)
                .build();
    }

    /**
     * 更新模型接入配置
     *
     * @param dto 模型接入配置信息
     * @return RestResult 结果
     */
    @PutMapping("/update")
    public RestResponse<ModelAccessConfig> updateModelAccessConfig(@Valid @RequestBody ModelAccessConfigDTO dto) {
        ModelAccessConfig accessConfig = modelAccessConfigService.updateModelAccessConfig(dto);
        return RestResponse.<ModelAccessConfig>builder()
                .data(accessConfig)
                .build();
    }

    /**
     * 删除模型接入配置
     *
     * @param id 主键ID
     * @return RestResult 结果
     */
    @DeleteMapping("/delete/{id}")
    public RestResponse<?> deleteModelAccessConfig(@PathVariable Long id) {
        modelAccessConfigService.deleteModelAccessConfig(id);
        return RestResponse.<ModelAccessConfig>builder()
                .build();
    }

    /**
     * 获取模型接入配置列表
     *
     * @param query 查询条件
     * @return RestResult 结果
     */
    @GetMapping("/list")
    public RestResponse<List<ModelAccessConfig>> getModelAccessConfigList(ModelAccessConfigQuery query) {
        List<ModelAccessConfig> modelAccessConfigs = modelAccessConfigService.getModelAccessConfigList(query);
        return RestResponse.<List<ModelAccessConfig>>builder()
                .data(modelAccessConfigs)
                .build();
    }

    /**
     * 启用/禁用模型接入配置
     *
     * @param id 主键ID
     * @param isActive 是否启用
     * @return RestResult 结果
     */
    @PutMapping("/enable-disable/{id}")
    public RestResponse<ModelAccessConfig> enableDisableModelAccessConfig(
            @PathVariable Long id,
            @RequestParam Integer isActive) {
        ModelAccessConfig modelAccessConfig = modelAccessConfigService.enableDisableModelAccessConfig(id, isActive);
        return RestResponse.<ModelAccessConfig>builder()
                .data(modelAccessConfig)
                .build();
    }
}